package servidor.localizacao;

import com.ekahau.sdk.Location;
import com.ekahau.sdk.LocationEstimate;
import com.ekahau.sdk.LocationEstimateListener;
import com.ekahau.sdk.LogicalArea;
import com.ekahau.sdk.LogicalAreaListener;
import com.ekahau.sdk.Status;
import com.ekahau.sdk.StatusListener;
import com.ekahau.sdk.TrackedDevice;

/**
 * @author Diego
 */
public class EkahauListener implements LocationEstimateListener, LogicalAreaListener, StatusListener {

    private static java.text.DecimalFormat format = new java.text.DecimalFormat("###,#00.00");

    /**
     * Receives a new LocationEstimate from the engine. This is called always
     * when location information is updated during tracking.
     * 
     * @param MovementVector the latest and previous location data
     * @param device the device involved
     */
    public synchronized void handleLocationEstimate(LocationEstimate locationEstimate, TrackedDevice device) {
        String ipAddress = (String) device.getAttribute("NETWORK.IP-ADDRESS");

        Location latest = locationEstimate.getLatestLocation();
//        Location accurate = locationEstimate.getAccurateLocation();
        ((Dispositivo) device).setLocation(latest);
//        System.out.println("Latest location for device " + ipAddress + ": X: " + format.format(latest.getX()) + " Y: "
//                + format.format(latest.getY()));
//        System.out.println("Older but more accurate location for device " + ipAddress + ": X: "
//                + format.format(accurate.getX()) + " Y: " + format.format(accurate.getY()));
    }

    /**
     * Receives an array of LogicalAreas from the engine. This is called always
     * when location information is updated during tracking.
     * 
     * @param LocationEstimate the estimated location
     * @param device the device involved
     */
    public synchronized void handleLogicalAreas(LogicalArea areas[], TrackedDevice device) {
        String ipAddress = (String) device.getAttribute("NETWORK.IP-ADDRESS");

        System.out.println("\nTop " + areas.length + " matching logical areas:");
        System.out.println("Received logical areas for device " + ipAddress);
        for (int i = 0; i < areas.length; i++) {
            LogicalArea area = areas[i];
            String probabilityPercent = format.format(area.getProbability() * 100);
            System.out.println("Area: " + area.getName() + " probability: " + probabilityPercent + " %");
            System.out.println("Logical area properties: ");
            System.out.println(area.getProperties());
        }
    }

    /**
     * Receives a status or error message from the engine.
     * 
     * @param status status of the device
     * @param device the device involved
     */
    public synchronized void handleStatus(Status status, TrackedDevice device) {
        String ipAddress = (String) device.getAttribute("NETWORK.IP-ADDRESS");

        System.out.println("Status for device " + ipAddress + ": " + status);
    }
}